找传奇、传世资源到传世资源站!

delphi串口通讯

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

进行串口接收数据进行解析from clipboard var
  len,i,offset,recvdlen:Integer;
  reqbuf:array[1..12]of Byte;
  recbuf:array[1..176]of Byte;
  recmem:TMemoryStream;
  chk:Byte;
  tmps:string;
  tmps1:string;
  recsl:TStringList;
  recfile:string;
  sendtimes:integer;
begin
//  pnlRecTimes.Caption:=Format('Download:%2f%% (%d-%d)',[100*recvdlen/len ,recvdlen ,len]) ;
  pnlRecTimes.Caption:='' ;
  Application.ProcessMessages;
  if not rs1.Connected then Exit;
    for i := 0 to 50 do //等待500ms
  begin
    TThread.Sleep(10);
    Application.ProcessMessages;
  end;
 {  if False then
  begin
    btn8.Enabled:=True;
    Application.MessageBox('It is forbidden to read the fault record during operation, please stop the fault record after reading it! '#13#10' (运行时禁止读取故障记录,请停机后再读取故障记录!)', 'Prompt(提示)', MB_OK MB_ICONINFORMATION);
    Exit;
  end;   }
  btn2.Enabled:=False;
  btn8.Enabled:=False;
  tmr1.Enabled:=False;
  reqbuf[ 1]:=$5A;
  reqbuf[ 2]:=$00;
  reqbuf[ 3]:=$40;
  reqbuf[ 4]:=$00;
  reqbuf[ 5]:=$00;
  reqbuf[ 6]:=$00;
  reqbuf[ 7]:=$00;
  reqbuf[ 8]:=$00;
  reqbuf[ 9]:=$00;
  reqbuf[10]:=$00;
  reqbuf[11]:=$00;
  reqbuf[12]:=Byte(0-($5A $40));
  rs1.WriteCommData(PAnsiChar(@reqbuf[1]),12);
  reqType := 2;
  RecBufNew:=False;
  i:=0;
  len:=0;
  while True do
  begin
    i:=i 1;
    if RecBufNew=True then
    begin
      len:=RecdBuf[5] RecdBuf[6]*$100 RecdBuf[7]*$10000;
      break;
    end;
    TThread.Sleep(10);
    Application.ProcessMessages;
    if(i>300)then
    begin
      btn8.Enabled:=True;
      btn2.Enabled:=True;
//      Application.MessageBox('Data extraction failed, please extract again! '#13#10' (数据提取失败,请再次提取!)', 'Prompt(提示)', MB_OK MB_ICONINFORMATION);
      break;
    end;
  end;
  if(i<300)then
  begin
    if len>0 then
    begin
      recmem:=TMemoryStream.Create;
      offset:=0;
      recvdlen:=0;
      //recmem.SetSize(len);
      while(recvdlen<len)do
      begin
        reqbuf[ 1]:=$5A;
        reqbuf[ 2]:=$00;
        reqbuf[ 3]:=$41;
        reqbuf[ 4]:=offset and $FF;
        reqbuf[ 5]:=(offset shr 8) and $FF;
        reqbuf[ 6]:=(offset shr 16) and $FF;
        reqbuf[ 7]:=(offset shr 24) and $FF;
        reqbuf[ 8]:=$B0;
        reqbuf[ 9]:=$00;
        reqbuf[10]:=$00;
        reqbuf[11]:=$00;
        chk:=0;
        for i := 1 to 11 do chk:=chk reqbuf[i];
        reqbuf[12]:=Byte(0-chk);
        rs1.WriteCommData(PAnsiChar(@reqbuf[1]),12);
        reqType := 3;
        RecBufNew:=False;
        i:=0;
        while True do
        begin
          i:=i 1;
          if RecBufNew=True then
          begin
            recmem.Write(@RecdBuf[5],RecLen);
            break;
          end;
          TThread.Sleep(10);  //100改为10
          Application.ProcessMessages;
          if(i>300)then
          begin
//            btn8.Enabled:=True;
//            btn2.Enabled:=True;
//            Application.MessageBox('Data extraction failed, please extract again! '#13#10' (数据提取失败,请再次提取!)', 'Prompt(提示)', MB_OK MB_ICONINFORMATION);
            break;
          end;
        end;
        if(i<300)then
        begin
          recvdlen:=recvdlen RecLen;
          offset:=recvdlen;
          pnlRecTimes.Caption:=Format('Download:%2f%% (%d-%d)',[100*recvdlen/len ,recvdlen ,len]) ;
          Application.ProcessMessages;
          sendtimes:=0;
        end
        else
        begin
          sendtimes:=sendtimes 1;
          if sendtimes>5 then
          begin
            ShowMessage('发送超时,CPU未响应');
            break;
          end;
        end;
      end;
      if(recmem.Size>0)then
      begin
        recsl:=TStringList.Create;
//        recsl.Add('时间,事件,交流输入,直流输入,输出电压,输出电流,充电电流,散热器温度,蓄电池温度,控制板温度,风扇输出');
//        recsl.Add('Time,Event,Input voltage,Boost input voltage,Boost input current 1,Boost input current 2,Boost output voltage,LLC output voltage,LLC output current,Inverter output voltage,Inverter output current,Inverter output frequency,Heat sink temperature,Inverter temperature,Fan duty cycle,SysStatusSignal,CPU Version,Inverter Version');
        tmps:='Time,Event,Input voltage,Boost input voltage,Boost input current 1,Boost input current 2,Boost output voltage,LLC output voltage,LLC output current,';
            tmps:=tmps 'Inverter output voltage,Inverter output current,Inverter output frequency,Heat sink temperature,Inverter temperature,Fan duty cycle,SysStatusSignal,CPU Version,Inverter Version';
        recsl.Add(tmps);
        recmem.Position:=0;
        while recmem.Position<recmem.Size do
        begin
          recmem.Read(recbuf[1],44);
          tmps:='';
          case recbuf[43] of
          $80: begin tmps:='DC input undervoltage' ; end;
            $B0: begin tmps:='DC input undervoltage recovery' ; end;
          $81: begin tmps:='DC input overvoltage' ; end;
            $B1: begin tmps:='DC input overvoltage recovery' ; end;
          $82: begin tmps:='Boost inductor L1 overcurrent' ; end;
            $B2: begin tmps:='Boost inductor L1 overcurrent recovery' ; end;
          $83: begin tmps:='Boost inductor L2 overcurrent' ; end;
            $B3: begin tmps:='Boost inductor L2 overcurrent recovery'   ; end;
          $84: begin tmps:='LLC output overvoltage'   ; end;
            $B4: begin tmps:='LLC output overvoltage recovery'     ; end;
          $85: begin tmps:='LLC primary side overcurrent'  ; end;
            $B5: begin tmps:='LLC primary side overcurrent recovery'; end;
          $86: begin tmps:='LLC output overcurrent' ; end;
            $B6: begin tmps:='LLC output overcurrent recovery'; end;
          $87: begin tmps:='AC output undervoltage'  ; end;
            $B7: begin tmps:='AC output undervoltage recovery'     ; end;
          $88: begin tmps:='AC output overvoltage' ; end;
            $B8: begin tmps:='AC output overvoltage recovery' ; end;
          $89: begin tmps:='AC output overcurrent' ; end;
            $B9: begin tmps:='AC output overcurrent recovery' ; end;
          $8A: begin tmps:='AC output short circuit' ; end;
            $BA: begin tmps:='AC output short circuit recovery' ; end;
          $8B: begin tmps:='Total hardware protection' ; end;
            $BB: begin tmps:='Total hardware protection recovery'   ; end;
          $8C: begin tmps:='Fan failure'   ; end;
            $BC: begin tmps:='Fan failure recovery'     ; end;
          $8D: begin tmps:='RS485 communication failure'  ; end;
           $BD: begin tmps:='RS485 communication failure recovery'; end;
          $8E: begin tmps:='Output failure' ; end;
            $BE: begin tmps:='Output failure recovery'; end;
          $8F: begin tmps:='Precharge failure'  ; end;
            $BF: begin tmps:='Precharge failure recovery'     ; end;

          $90: begin tmps:='DC/DC over temperature' ; end;
            $C0: begin tmps:='DC/DC over temperature recovery'   ; end;
          $91: begin tmps:='Accelerating overcurrent'   ; end;
            $C1: begin tmps:='Accelerating overcurrent recovery'     ; end;
          $92: begin tmps:='Accelerating overvoltage'  ; end;
            $C2: begin tmps:='Accelerating overvoltage recovery'; end;
          $93: begin tmps:='Inverter over temperature' ; end;
            $C3: begin tmps:='Inverter over temperature recovery'; end;
          $94: begin tmps:='Inverter overload'  ; end;
            $C4: begin tmps:='Inverter overload recovery'     ; end;
          $95: begin tmps:='Current detection fault' ; end;
            $C5: begin tmps:='Current detection fault recovery'   ; end;
          $96: begin tmps:='Short circuit to ground'   ; end;
            $C6: begin tmps:='Short circuit to ground recovery'     ; end;
          $97: begin tmps:='Wave-by-wave current limiting fault'  ; end;
            $C7: begin tmps:='Wave-by-wave current limiting fault recovery'; end;
          $98: begin tmps:='Discharge relay failure' ; end;
            $C8: begin tmps:='Discharge relay failure recovery'; end;
          $99: begin tmps:='Main relay failure'  ; end;
            $C9: begin tmps:='Main relay failure recovery'     ; end;
          end;
          with recsl do
          begin
            tmps1:=IntToStr(2000 (recbuf[4] shr 2)) '-';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(((recbuf[4] shl 2)or(recbuf[3] shr 6))and $0F)]), ' ', '0') '-';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[((recbuf[3] shr 1)and $1F)]),' ','0') ' ';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(((recbuf[3] shl 4)or(recbuf[2] shr 4))and $1F)]),' ','0') ':';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(((recbuf[2] shl 2)or(recbuf[1] shr 6))and $3F)]),' ','0') ':';
            tmps1:=tmps1 ReplaceStr(Format('%02d',[(recbuf[1] and $3F)]),' ','0');
            tmps:=#39 tmps1 ',' tmps;
            tmps1:=Format('%4.1fV',[(recbuf[9] recbuf[10]*$100)/10]); //Vin
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[11] recbuf[12]*$100)/10]); //Boost_Vin
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[13] recbuf[14]*$100)/10]);//Boost_Iin1
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[15] recbuf[16]*$100)/10]);//Boost_Iin2
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[17] recbuf[18]*$100)/10]);//Boost_Vout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[19] recbuf[20]*$100)/10]); //LLC_Vout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[21] recbuf[22]*$100)/10]); //LLc_Iout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fV',[(recbuf[23] recbuf[24]*$100)/10]);//Inverter_Vout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fA',[(recbuf[25] recbuf[26]*$100)/10]);//Inverter_Iout
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%4.1fHz',[(recbuf[27] recbuf[28]*$100)/10]);//Inverter_Hz
            tmps:=tmps ',' tmps1;
            tmps1:=IntToStr(Smallint(recbuf[29] recbuf[30]*$100)) '℃';  //散热器
            tmps:=tmps ',' tmps1;
            tmps1:=IntToStr(Smallint(recbuf[31] recbuf[32]*$100)) '℃'; //逆变器
            tmps:=tmps ',' tmps1;
            tmps1:=Format('%d%%',[(recbuf[33] recbuf[34]*$100)]);//风扇
            tmps:=tmps ',' tmps1;
            tmps1:=UIntToStr(recbuf[35]);    //SysStatusSignal
            tmps:=tmps ',' tmps1;
//            tmps1:='V' UIntToStr(recbuf[37] /10);    //0.1 DC/DC software version
            tmps1:='V' Format('%4.1f',[(recbuf[37] recbuf[38]*$100)/10]);//
            tmps:=tmps ',' tmps1;
//            tmps1:='V' UIntToStr(recbuf[39] /10);    //0.1 DC/AC software version
            tmps1:='V' Format('%4.1f',[(recbuf[39] recbuf[40]*$100)/10]);//
            tmps:=tmps ',' tmps1;
            Add(tmps);  //换行(注意:换行用Add,换列用,)
          end;
        end;
        recfile:=ExtractFilePath(Application.ExeName) 'Rec' FormatDateTime('YYYYMMDDHHNNSS',Now()) '.csv';
        recsl.SaveToFile(recfile);
        recsl.Free;
//        Application.MessageBox(PChar('记录文件下载成功,保存为' recfile ',请用Excel打开查看。'), '下载成功', MB_OK
//          MB_ICONINFORMATION);
        Application.MessageBox(PChar('The log file was successfully downloaded and saved as' recfile ',Please open it in Excel.'), 'download successful', MB_OK
          MB_ICONINFORMATION);

      end;
      recmem.SaveToFile(ExtractFilePath(Application.ExeName) 'recfile.bin');
      recmem.Free;
    end
//    else Application.MessageBox('没有故障记录!', '提示', MB_OK MB_ICONINFORMATION);
    else Application.MessageBox('No fault record!', 'Prompt', MB_OK MB_ICONINFORMATION);

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复